package com.visikard.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;

import com.visikard.system.Device;
import com.visikard.ui.R;
import com.visikard.ui.activity.CouponBuilderActivity;
import com.visikard.utility.BitmapUtils;
import com.visikard.utility.UILImageUtils;

public class CBPreviewBuilderFragment extends BaseFragment implements OnClickListener {

	public static final String TAG = "CBPreviewBuilderFragment";
	
	ImageView coupon_image, home_btn_menu;
	TextView name, percent_off, expire_date;
	Button btn_save;
	CheckBox chk_confirm;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.cb_preview, container, false);
		coupon_image = (ImageView) v.findViewById(R.id.coupon_image);
		name = (TextView) v.findViewById(R.id.name);
		percent_off = (TextView) v.findViewById(R.id.percent_off);
		expire_date = (TextView) v.findViewById(R.id.expire_date);
		btn_save = (Button) v.findViewById(R.id.btn_save);
		chk_confirm = (CheckBox) v.findViewById(R.id.chk_confirm);
		home_btn_menu = (ImageView) v.findViewById(R.id.home_btn_menu);
		
		btn_save.setOnClickListener(this);
		home_btn_menu.setOnClickListener(this);
		
		return v;
	}

	@Override 
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		//new Coupon
		loadData();
	}
	
	private void loadData(){
		if(CouponBuilderActivity.itemCouponBuilderCreate != null){
			if(CouponBuilderActivity.couponBitmap != null){
				coupon_image.setImageBitmap(CouponBuilderActivity.couponBitmap);
			}
			else{
//				ImageLoader.getInstance().loadImage(coupon_image, CouponBuilderActivity.itemCouponBuilderCreate.getImageSpecial(),
//						Device.getDeviceWidth(), BitmapUtils.ROUND_NONE, 0, 0, 0, 0, 0);
				UILImageUtils.loadImage(coupon_image, CouponBuilderActivity.itemCouponBuilderCreate.getImageSpecial(), 0, BitmapUtils.ROUND_NONE, 0, 0, 0, 0, 0);
			}
			LinearLayout.LayoutParams params = (LayoutParams) coupon_image.getLayoutParams();
			params.width = Device.getDeviceWidth();
			params.height = params.width / 2;
			name.setText(CouponBuilderActivity.itemCouponBuilderCreate.getCouponName());
			percent_off.setText(getString(R.string.percent_off_text,""+CouponBuilderActivity.itemCouponBuilderCreate.getCampaignDetailValue() + "%"));
			expire_date.setText(getString(R.string.expire_date_text, CouponBuilderActivity.itemCouponBuilderCreate.getExpireDate()));
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_save:
			if(chk_confirm.isChecked()){
				if(CouponBuilderActivity.itemCouponBuilderCreate.getCouponID() > 0)
					((CouponBuilderActivity)getActivity()).updateCoupon();
				else
					((CouponBuilderActivity)getActivity()).createCoupon();
			}
			else{
				Toast.makeText(getActivity(), getString(R.string.alert_confirm_check), Toast.LENGTH_SHORT).show();
			}
			break;

		case R.id.home_btn_menu:
			((CouponBuilderActivity)getActivity()).onBackPressed();
			break;
		default:
			break;
		}
	}

}
